package com.aspire.moss.portal.desktop;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Toast;

import com.aspire.moss.portal.desktop.AppView.OnDeleteClickListener;
import com.aspire.moss.portal.desktop.DragLayer.OnMoveIconResetCallBack;

/**
 * 用户整个应用的事件处理
 * 
 * @author Administrator
 * 
 */
public class EventCenter implements OnClickListener, OnLongClickListener,
		OnDeleteClickListener, DragLayer.onInterceptTouchEvent , OnMoveIconResetCallBack{

	private static final String TAG = EventCenter.class.getSimpleName();

	/**
	 * 整个桌面的总布局,当用户长按时拖动图标,将应用加在最上面,并处理
	 */
	private MoveIconController moveIconCtrl;

	/**
	 * 是否长按住了图标,标识下一步的拖动
	 */
	public static boolean isLongClick;

	private Context mContext;

	private WorkspaceController wspCtrl;

	/**
	 * 当用户的手指在屏幕两边时,标志是否启动一个任务监听 当用户把图标移走了,标志是否启动一个监听
	 */
	private boolean startListenterMovePage, startListenerMoveGone;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			initMoveView((AppView) msg.obj);
		}

	};

	public EventCenter(Context mContext, WorkspaceController wspCtrl,
			MoveIconController moveIconController) {
		super();
		this.mContext = mContext;
		this.wspCtrl = wspCtrl;
		this.moveIconCtrl = moveIconController;
	}

	@Override
	public boolean onLongClick(View view) {
		wspCtrl.lockWorkspace();
		/**
		 * 发送消息到handler处理
		 */
		handler.sendMessageDelayed(handler.obtainMessage(0, view), 300);
		return true;
	}

	@Override
	public void onDeleteClick(AppView view) {
		Utils.createDeleteDailog(mContext, wspCtrl, view.appInfo);
	}

	@Override
	public void onClick(View view) {
		Toast.makeText(mContext, "我点了", 50).show();
	}

	@Override
	public boolean onDragLayeronIntercept(MotionEvent event) {
		Log.d(TAG, "onDragLayerTouchEvent event=" + event);
		final float rawX = event.getX();
		final float rawY = event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			setLongClickViewLocation(rawX, rawY);
			listenerMovePage();
			listenerMove();
//			listenerMoveGone();
			break;
		case MotionEvent.ACTION_UP:
//			moveLongClickViewUp();
		case MotionEvent.ACTION_CANCEL:
			resetMoveView();
		default:
			break;
		}
		return true;
	}

	/**
	 * 初始化拖动图标
	 */
	private void initMoveView(AppView longClickView) {
		isLongClick = true;
		moveIconCtrl.initMoveIcon(longClickView);
	}

	/**
	 * 重置拖动变量
	 */
	private void resetMoveView() {
		isLongClick = false;
		startListenerMoveGone = false;
		moveIconCtrl.resetMoveIcon(0, 0, null, null, null, null);
	}
	
	/**
	 * 设置拖动图标的位置
	 * 
	 * @param rawX
	 * @param rawY
	 */
	private void setLongClickViewLocation(float rawX, float rawY) {
		moveIconCtrl.moveIcon(rawX, rawY);
	}

	/**
	 * 移动appview
	 * @throws CloneNotSupportedException 
	 * 
	 * */
	/*private void moveLongClickViewUp(){
		if (null != moveIconCtrl) {
			final AppInfo appInfo = moveIconCtrl.getAppInfo();
			final int page_no = wspCtrl.getCurrentScreen();
			final CellLayout cellLayout = wspCtrl.findScreen(page_no);
			if(appInfo.page_no == page_no){
				currentPageMove(appInfo, cellLayout);
			}else{
				//这里一定是翻页了
				movePageMove(appInfo , cellLayout);
			}
		}

	}*/
	 
	/**
	 * 在当页面交换位置
	 * @throws CloneNotSupportedException 
	 */
	/*private void currentPageMove(AppInfo appInfo, final CellLayout cellLayout) {
		Log.d(TAG, "currentPageMove");
		// 找到手指松开的地方的cellXY
		final AppView view = (AppView) cellLayout.isExists(moveIconCtrl
				.getMoveIconX(), moveIconCtrl.getMoveIconY(), moveIconCtrl
				.getMoveIconWidth(), moveIconCtrl.getMoveIconHeight());
		AppInfo target = null;
		AppViewAction action = null;
		if (null != view) {
			//当前页的俩个应用交换位置
			target = view.appInfo.cloneAppInfo();
			cellLayout.moveToXY(view, appInfo.cellX, appInfo.cellY, true);
			action = AppViewAction.SWAP;
		} else {
			final int cellXY[] = cellLayout.findLastAppView();
			target = (AppInfo) appInfo.cloneAppInfo();
			if(null !=cellXY){
				final int clearUpX = appInfo.cellX;
				final int clearUpY = appInfo.cellY;
				cellLayout.clearUp(clearUpX, clearUpY, true);
				target.cellX = cellXY[0];
				target.cellY = cellXY[1];
				action = AppViewAction.MOVE;
			}else{
				action = AppViewAction.RESET;
			}
		}
		resetLongClickView(appInfo, target, cellLayout , action);
	}*/
	
	/**
	 * 在不同的页面交换
	 * @param appInfo
	 * @param cellLayout
	 */
	/*private void movePageMove(AppInfo appInfo, final CellLayout cellLayout) {
		Log.d(TAG, "movePageMove");
		// 找到手指松开的地方的cellXY
		final AppView view = (AppView) cellLayout.isExists(moveIconCtrl
				.getMoveIconX(), moveIconCtrl.getMoveIconY(), moveIconCtrl
				.getMoveIconWidth(), moveIconCtrl.getMoveIconHeight());
		AppInfo target = null;
		AppViewAction action = null;
		if (null != view) {
			//两个不同页面的应用交换
			target = view.appInfo.cloneAppInfo();
			action = AppViewAction.SWAP;
		} else {
			int cellXY [] =cellLayout.findLastEmptyAppView();
			target = (AppInfo) appInfo.cloneAppInfo();
			if(null != cellXY){
				//新页添加
				target.cellX = cellXY[0];
				target.cellY = cellXY[1];
				target.screen = cellLayout.screenId;
				action = AppViewAction.MOVE;
			}else{
				//如果cellXY返回null一定是没有空间了,所以应用还原
				action = AppViewAction.RESET;
			}
		}
		resetLongClickView(appInfo, target, cellLayout , action);
	}*/

	/**
	 * 当用户的手指在屏幕两边的时候 ,看看用户是否是想翻页, 条件是,在1秒以后用户的手指还在这里
	 */
	private void listenerMovePage() {
		Log.i(TAG, "moveIconX=" + moveIconCtrl.getMoveIconX());
		final boolean onLeft = moveIconCtrl.getMoveIconX() < -5 ;
		final int moveIconRight = moveIconCtrl.getMoveIconX() + moveIconCtrl.getMoveIconWidth();
		final boolean onRight = moveIconRight > Utils.getScreenWidth(mContext);
		Log.i(TAG, "onRight" + onRight);
		if (!startListenterMovePage && (onLeft || onRight)) {
			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					Log.d(TAG, "start listener postion run");
					longClickMovePage(moveIconCtrl.getMoveIconX(), moveIconCtrl
							.getMoveIconWidth());
				}

			}, Config.MOVE_WAIT_TIME);
			startListenterMovePage = true;
			Log.d(TAG, "start listener postion");
		}
	}

	/**
	 * 监听用户把图标移走了,当前这个不是为空格吗?,空格后面的控件向前一步走
	 */
	private void listenerMoveGone() {
		if (!startListenerMoveGone) {
			
		}
	}
	
	/**
	 * 监听移动时,要动态移动其它的图标
	 */
	private void listenerMove(){
		
	}

	/**
	 * 当长按时 移动屏幕翻页
	 * 
	 * @param x
	 *            当前的坐标
	 * @param width
	 *            图标的宽度
	 */
	private void longClickMovePage(int x, int width) {
		if (x < 0) {
			wspCtrl.scrollRight();
		} else if (x + width > Utils.getScreenWidth(mContext)) {
			if (Workspace.MAX_PAGE - 1 > wspCtrl.getScreens()
					&& wspCtrl.getCurrentScreen() == wspCtrl
							.getScreens() - 1) {
				wspCtrl.addCelllayoutView();
			}
			wspCtrl.scrollLeft();
		}
		startListenterMovePage = false;
	}

	@Override
	public void onResetEnd(AppInfo appInfo , AppInfo target , AppViewAction action) {
		switch (action) {
		case MOVE:
			wspCtrl.moveAppView(appInfo, target);
			break;
		case SWAP:
			wspCtrl.swapAppView(appInfo, target);
			break;
		case RESET:
			wspCtrl.resetAppView(appInfo);
			break;

		}
	}

	/**
	 * 手指放下时,复原图标
	 * @param appInfo
	 * @param target
	 * @param cellLayout
	 * @param isMoveOrSwap
	 */
	/*private void resetLongClickView(AppInfo appInfo, AppInfo target,
			CellLayout cellLayout , AppViewAction action) {
		moveIconCtrl.resetMoveIcon(cellLayout.getMChildLeft(target.cellX),
				cellLayout.getMChildTop(target.cellY), this , appInfo , target , action);

	}*/
}
